//
//  WBBaseViewController.swift
//  Weibo
//
//  Created by 伏文东 on 2018/1/22.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {
    
    /// 访客视图信息字典
    var visitorDictoinaryInfo: [String: String]?
    
    var tableView: UITableView?
    // 刷新控件
    var refreshControl: UIRefreshControl?
    // 上拉刷新标记
    var isPullup = false
    
    /// 自定义导航条
    lazy var navigationBar = WBSecondNavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    /// 自定义的导航条目 - 以后设置导航栏内容，统一使用 navItem
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        WBNetworkManager.shared.userLogon ? loadData() : ()
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(loginSuccess),
            name: NSNotification.Name(rawValue: WBUserLoginSuccessedNotification),
            object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override var title: String? {
        didSet {
            navItem.title = title
        }
    }
    // 加载数据 - 具题的实现由子类负责
    @objc func loadData() {
        
        refreshControl?.endRefreshing()
    }
}
// MARK:- 访客视图监听方法
extension WBBaseViewController {
    
    @objc private func loginSuccess(n: Notification) {
        
        view = nil
        
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        // 注销通知 -》
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc private func login() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserShouldLoginNotification), object: nil)
        
    }
    @objc private func register() {
        print("用户注册 ")
    }
}

// MARK:- 设置界面
extension WBBaseViewController {
    private func setupUI() {
        view.backgroundColor = UIColor.cz_random()
        //取消自动缩进
        automaticallyAdjustsScrollViewInsets = false
        setupnavigationBar()
        WBNetworkManager.shared.userLogon ? setupTableView() : setupVisitorView()
    }
    
    @objc func setupTableView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navigationBar)
        
        //设置数据源和代理-？ 目的： 子类直接是下面数据元的方法和协议
        tableView?.delegate = self
        tableView?.dataSource = self
        tableView?.contentInset = UIEdgeInsets(top: 44, left: 0, bottom: 0, right: 0)
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    /// 设置访客视图
    private func setupVisitorView() {
        let visitorView = WBVisitorView(frame: view.bounds)
        
        view.insertSubview(visitorView, belowSubview: navigationBar)
        
        // 设置访客信息
        visitorView.visitorInfo = visitorDictoinaryInfo
        
        // 设置访客视图按钮的监听方法
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        // 设置导航按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))
    }
    private func setupnavigationBar() {
        // 添加导航条
        view.addSubview(navigationBar)
        // 将 item 设置给 bar
        navigationBar.items = [navItem]
        
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        navigationBar.tintColor = UIColor.orange
        navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.darkGray]
    }
}
// MARK:- 遵守数据源道理协议
extension WBBaseViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //1. 判断是否是最后一行
        let row = indexPath.row
        let section = tableView.numberOfSections - 1

        if row < 0 || section < 0 {return}
        let count = tableView.numberOfRows(inSection: section)
        if row == (count - 1) && !isPullup {
            isPullup = true
            // 开始刷新
            loadData()
        }
    }
}





























